package com.sample.generator.io;

import java.io.IOException;
import java.io.InputStream;

import com.sample.generator.util.ClassUtil;

public class ResourceInputStreamProvider implements InputStreamProvider {

	private Class<?> base;

	public ResourceInputStreamProvider(Class<?> base) {

		if (base == null) {
			throw new IllegalArgumentException("Argument base is null.");
		}
		this.base = base;
	}

	public InputStream open(String name) throws IOException {

		if (name == null) {
			throw new IllegalArgumentException("Argument name is null.");
		}
		name = name.replace('\\', '/');
		if (name.startsWith("/")) {
			name = name.substring(1);
		} else {
			name = base.getPackage().getName().replace('.', '/') + '/' + name;
		}
		return ClassUtil.getResourceAsStream(name);
	}
}
